﻿
#include "DIALOG.h"
#include "FileOperation.h"
//#include "PopEdit.h"
#include "CreateWidget.h"
#include "AppFunction.h"




/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_WINDOW_0 (GUI_ID_USER + 0x00)
#define ID_BUTTON_0 (GUI_ID_USER + 0x01)
#define ID_BUTTON_1 (GUI_ID_USER + 0x02)
#define ID_BUTTON_2 (GUI_ID_USER + 0x03)
#define ID_BUTTON_3 (GUI_ID_USER + 0x04)


#define COLOR_CONV GUICC_M565

// USER START (Optionally insert additional defines)
// USER END



// USER START (Optionally insert additional static data)

static void *pImage1 = NULL, *pImage2 = NULL, *pImage3 = NULL, *pImage4 = NULL;
//static WM_HWIN homePage, navigatePage;
static pPARA_TCB  pHomePage, pNavigatePage, pCurrentSubPage, pPreviousPage, pEncryptionPage;
static uint16_t  currentLanguage = LANGUAGE_CHINESE;
GUI_BITMAP bitmapOn, bitmapOff;
GUI_LOGPALETTE paletteOn, paletteOff;
void(*pTimerHomePage)(void);
// USER END

/*********************************************************************
*
*       _aDialogCreate
*/
static  GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
  { WINDOW_CreateIndirect, "Window", ID_WINDOW_0, 0, 0, 720, 720, 0, 0x0, 0 },
  { BUTTON_CreateIndirect, "Button", ID_BUTTON_0, 10, 80, 64, 64, 0, 0x0, 0 },
  { TEXT_CreateIndirect, "TEXT", ID_BUTTON_1, 100, 10, 64, 64, 0, 0x0, 0 },
  { SLIDER_CreateIndirect, "SLIDER", ID_BUTTON_2, 200, 80, 64, 64, 0, 0x0, 0 },
  { PROGBAR_CreateIndirect, "PROGBAR", ID_BUTTON_3, 440, 80, 64, 64, 0, 0x0, 0 },
  // USER START (Optionally insert additional widgets)
  // USER END
};

// USER START (Optionally insert additional static code)
extern GUI_CONST_STORAGE  GUI_FONT  GBK_16m16;
extern GUI_CONST_STORAGE  GUI_FONT  GBK_24m24;
extern GUI_CONST_STORAGE  GUI_FONT  GBK_48m48;
// USER END

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialogFrame(WM_MESSAGE * pMsg) {
	static int onOff = 0;
	int NCode;
	int Id;

	switch (pMsg->MsgId) {
	case WM_PAINT:
		break;
	default:
		WM_DefaultProc(pMsg);
		break;
	}
}


void SetLanguage(uint16_t language)
{
	currentLanguage = language;

	;
}


void SetEncryptionPage(pPARA_TCB pPage)
{
	pEncryptionPage = pPage;
}


void Concatenate(pPARA_TCB pPara, uint16_t number, uint8_t layer)
{
	uint16_t count = 0;

	number--;
	do
	{
		pPara[count].winLayer = layer;

		if (count < number)
			pPara[count].pParallelNext = &pPara[count + 1];
		else
		{
			pPara[count].pParallelNext = NULL;
			break;
		}
	} while (++count);
}


void ConfigureBackGradient(pPARA_TCB pPara)
{
	GUI_MEMDEV_Handle hMemdev;

	hMemdev = GUI_MEMDEV_Create(0, 0, pPara->guiTCB.xSize, pPara->guiTCB.ySize);
	//    hMemdev = GUI_MEMDEV_CreateFixed(0, 0, pPara->guiTCB.xSize, pPara->guiTCB.ySize, GUI_MEMDEV_HASTRANS, GUI_MEMDEV_APILIST_16, COLOR_CONV);

	if (hMemdev > 0)
	{
		GUI_MEMDEV_Select(hMemdev);
		GUI_Clear();
		GUI_DrawGradientH(0, 0, pPara->guiTCB.xSize, pPara->guiTCB.ySize, GUI_MAKE_COLOR(0xA02020), GUI_BLACK);// GUI_MAKE_COLOR(0xC33764), GUI_MAKE_COLOR(0x1D2671));
		GUI_MEMDEV_Select(0);
		pPara->guiTCB.pictureBack.hMem = hMemdev;

	}
}


void SetPictureHmem(pPARA_TCB pPara, GUI_HMEM on, GUI_HMEM off, GUI_HMEM back)
{
	pPara->guiTCB.pictureOn.hMem   = on;
	pPara->guiTCB.pictureOff.hMem  = off;
	pPara->guiTCB.pictureBack.hMem = back;

	//pPara->guiTCB.pCurrentPicture = (on != 0 || off != 0);
}


uint8_t ConfigureBitMap(uint8_t *pPath, pBitMap pMap, uint8_t backPicture)
{
	uint32_t size;
	uint8_t *pBuffer;
	int8_t  ret = 0;
	GUI_HMEM hMem;
	GUI_MEMDEV_Handle hMemdev;

	pBuffer = GetPictureData(pPath, &size, &hMem);

	if (pBuffer != NULL)
	{
		ret = !GUI_CreateBitmapFromStream(&pMap->bitMap, &pMap->palette, pBuffer);

		if (backPicture == 1)
		{
			//			hMemdev = GUI_MEMDEV_CreateFixed(0, 0, pMap->bitMap.XSize, pMap->bitMap.YSize, GUI_MEMDEV_HASTRANS, GUI_MEMDEV_APILIST_16, COLOR_CONV);
			hMemdev = GUI_MEMDEV_CreateEx(0, 0, pMap->bitMap.XSize, pMap->bitMap.YSize, GUI_MEMDEV_HASTRANS);

			if (ret == 1 && hMemdev > 0)
			{
				GUI_MEMDEV_Select(hMemdev);
				GUI_Clear();
				GUI_DrawBitmap(&pMap->bitMap, 0, 0);
				GUI_MEMDEV_Select(0);
				pMap->hMem = hMemdev;

			}
			else
				GUI_MEMDEV_Delete(hMemdev);

			GUI_ALLOC_Free(hMem);
		}
		else
		{
			if (ret)
				pMap->hMem = hMem;
			else
				pMap->hMem = 0;
		}
	}


	//    if (pBuffer != NULL)
	//	{
	//		ret = !GUI_CreateBitmapFromStream(&pMap->bitMap, &pMap->palette, pBuffer);

	//		if (ret)
	//			pMap->hMem = hMem;
	//		else
	//			pMap->hMem = 0;
	//	}

	return ret;
}


uint8_t SetInitialBitMap(pPARA_TCB pPara, pBitMap pMap)
{
	uint8_t ret = 1;

	pPara->guiTCB.pCurrentPicture = &pMap->bitMap;

	return ret;
}


void SetHomePageTimerCallback(void(*pTimer)(void))
{
	pTimerHomePage = pTimer;
}

void SetHomePage(pPARA_TCB pPage)
{
	pHomePage = pPage;
}


void SetHomePageMovingFlag(uint8_t state)
{
	pHomePage->guiTCB.isMoving = state;
}


void SetNavigationPage(pPARA_TCB pPage)
{
	pNavigatePage = pPage;
}


static int ButtonRedrawSkin(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
	WM_HWIN hWin;
	int x0, y0;
	int IsPressed;
	static int onOff = 0;
	pPARA_TCB pPara;
	GUI_MEMDEV_Handle hMemdev;
	GUI_RECT rect;


	switch (pDrawItemInfo->Cmd)
	{
	case WIDGET_ITEM_CREATE:
		//case WIDGET_ITEM_DRAW_BACKGROUND:
	case WIDGET_ITEM_DRAW_BITMAP:
		//case WIDGET_ITEM_DRAW_TEXT:
		break;
	default: return BUTTON_DrawSkinFlex(pDrawItemInfo);

	case WIDGET_ITEM_DRAW_BACKGROUND:

		WM_GetWindowRect(&rect);

		if ((rect.x0 + rect.x1) < 0 || rect.x0 >= 720)
			break;

		BUTTON_GetUserData(pDrawItemInfo->hWin, &pPara, sizeof(pPARA_TCB));

		if (pPara->appTCB.guiState == 1)
		{
			x0 = (pDrawItemInfo->x1 - pPara->guiTCB.pictureOn.bitMap.XSize) / 2;
			GUI_DrawBitmap(&pPara->guiTCB.pictureOn.bitMap, x0, 0);
			//GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureOn.hMem, rect.x0 + x0, rect.y0);
		}
		else
		{
			x0 = (pDrawItemInfo->x1 - pPara->guiTCB.pictureOff.bitMap.XSize) / 2;
			GUI_DrawBitmap(&pPara->guiTCB.pictureOff.bitMap, x0, 0);
			//GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureOff.hMem, rect.x0 + x0, rect.y0);
		}

		break;
	case WIDGET_ITEM_DRAW_TEXT:
		return BUTTON_DrawSkinFlex(pDrawItemInfo);
		break;
	}
	return 0;
}


static int SliderRedrawSkin(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
	WM_HWIN hWin;
	int x0, y0;
	int IsPressed;
	static int onOff = 0;
	pPARA_TCB pPara;
	GUI_MEMDEV_Handle hMemdev;
	GUI_RECT rect;

	WM_GetWindowRect(&rect);

	if ((rect.x0 + rect.x1) < 0 || rect.x0 >= 720)
		return 0;

	SLIDER_GetUserData(pDrawItemInfo->hWin, &pPara, sizeof(pPARA_TCB));


	switch (pDrawItemInfo->Cmd)
	{
	case WIDGET_ITEM_CREATE:
		WM_GetWindowRect(&rect);
		break;
	case WIDGET_ITEM_DRAW_FOCUS:
		break;
	default: 
		return SLIDER_DrawSkinFlex(pDrawItemInfo);
	case WIDGET_ITEM_DRAW_SHAFT:
		GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureBack.hMem, rect.x0, rect.y0 + (pDrawItemInfo->y1 - pPara->guiTCB.pictureBack.bitMap.YSize) / 2);

		break;
	case WIDGET_ITEM_DRAW_THUMB:
		if (((SLIDER_SKINFLEX_INFO *)(pDrawItemInfo->p))->IsPressed == 1)
		{
			GUI_DrawBitmap(&pPara->guiTCB.pictureOn.bitMap, pDrawItemInfo->x0, pDrawItemInfo->y0);
			//GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureOn.hMem, rect.x0 + x0, rect.y0);
		}
		else
		{
			GUI_DrawBitmap(&pPara->guiTCB.pictureOff.bitMap, pDrawItemInfo->x0, pDrawItemInfo->y0);
			//GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureOff.hMem, rect.x0 + x0, rect.y0);
		}

		break;
	case WIDGET_ITEM_DRAW_TICKS:
		break;
	}
	return 0;
}


static int PROGBARRedrawSkin(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
	WM_HWIN hWin;
	int x0, y0;
	int IsPressed;
	static int onOff = 0;
	pPARA_TCB pPara;
	GUI_MEMDEV_Handle hMemdev;
	GUI_RECT rect;

	WM_GetWindowRect(&rect);

	//if ((rect.x0 + rect.x1) < 0 || rect.x0 >= 720)
	//	return 0;

	PROGBAR_GetUserData(pDrawItemInfo->hWin, &pPara, sizeof(pPARA_TCB));


	switch (pDrawItemInfo->Cmd)
	{
	case WIDGET_ITEM_CREATE:
		PROGBAR_SetValue(pDrawItemInfo->hWin, 30);
		break;
	default:
		return PROGBAR_DrawSkinFlex(pDrawItemInfo);
	case WIDGET_ITEM_DRAW_BACKGROUND:
		GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureBack.hMem, rect.x0, rect.y0);
		break;
	case WIDGET_ITEM_DRAW_FRAME:
		//return PROGBAR_DrawSkinFlex(pDrawItemInfo);
		break;
	case WIDGET_ITEM_DRAW_TEXT:
		return PROGBAR_DrawSkinFlex(pDrawItemInfo);
	}
	return 0;
}


void DeleteSubPage(WM_HWIN hWin, pPARA_TCB pPara)
{
	pPARA_TCB pBase = pPara;

	while (1)
	{
		pPara = pPara->pParallelNext;

		if (pPara != NULL)
		{
			pPara->guiState = GUI_STATE_INVALID;
			WM_DeleteWindow(WM_GetDialogItem(pPara->parent, pPara->guiTCB.id));
			pPara->guiTCB.pCurrentPicture = NULL;
			GUI_ALLOC_Free(pPara->guiTCB.pictureOn.hMem);
			GUI_ALLOC_Free(pPara->guiTCB.pictureOff.hMem);
			//GUI_MEMDEV_Delete(pPara->guiTCB.pictureOn.hMem);
			//GUI_MEMDEV_Delete(pPara->guiTCB.pictureOff.hMem);
		}
		else
		{
			WM_DeleteWindow(hWin);
			pBase->guiTCB.pCurrentPicture = NULL;
			//GUI_MEMDEV_Delete(pBase->guiTCB.pictureBack.hMem);

			break;
		}
	}
}


static int _OwnerDraw(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
	switch (pDrawItemInfo->Cmd) {
	case WIDGET_DRAW_OVERLAY:
		GUI_SetColor(GUI_RED);
		GUI_DrawHLine(40, 0, 99);
		GUI_DrawHLine(59, 0, 99);
		break;
	default:
		return LISTWHEEL_OwnerDraw(pDrawItemInfo);
	}
	return 0;
}


void CreateSubWindow(WM_HWIN hWin, pPARA_TCB pPara)
{
	pPARA_TCB pBase;

	pBase = pPara->pNextLayer;
	if (pPara->guiTCB.winType == WIDGET_BUTTON_SLIDE && pBase != NULL && pBase->guiTCB.winType == WIDGET_WINDOW)
		CreateWidget(pBase, hWin, 1);
}


void MoveNavigationWindow(pPARA_TCB pPara)
{
	pPARA_TCB pBase;
	pBase = pNavigatePage;

	while (1)
	{
		pBase = pBase->pParallelNext;
		if (pBase != NULL)
		{
			if (pBase->pNextLayer == pPara)
			{
				WM_MoveTo(pNavigatePage->parent, -pBase->guiTCB.xPos, 0);
				break;
			}
		}
		else
			break;
	}
}


void SetFontAndAlignAndParameter(WM_HWIN hItem, pPARA_TCB pPara)
{
	void(*pFuncAlign)(WM_HMEM hObj, int Align) = NULL;
	void(*pFuncFont)(WM_HMEM hObj, const GUI_FONT * pFont) = NULL;
	void(*pFuncPara)(WM_HMEM hObj, const void * pSrc, int NumBytes) = NULL;


	switch (pPara->guiTCB.winType)
	{
	case WIDGET_BUTTON:
	case WIDGET_BUTTON_SLIDE:
		pFuncAlign = BUTTON_SetTextAlign;
		pFuncFont  = BUTTON_SetFont;
		pFuncPara  = BUTTON_SetUserData;
		break;
	case WIDGET_STATIC_TEXT:
		pFuncAlign = TEXT_SetTextAlign;
		pFuncFont  = TEXT_SetFont;
		pFuncPara  = TEXT_SetUserData;
		break;
	case WIDGET_EDIT:
		pFuncAlign = EDIT_SetTextAlign;
		pFuncFont  = EDIT_SetFont;
		pFuncPara  = EDIT_SetUserData;
		break;
	case WIDGET_SLIDE:
		pFuncPara  = SLIDER_SetUserData;
		break;
	case WIDGET_PROGBAR:
		pFuncAlign = PROGBAR_SetTextAlign;
		pFuncFont  = PROGBAR_SetFont;
		pFuncPara  = PROGBAR_SetUserData;
		break;
	case WIDGET_LIST_WHEEL:
		pFuncAlign = LISTWHEEL_SetTextAlign;
		pFuncFont  = LISTWHEEL_SetFont;
		pFuncPara  = LISTWHEEL_SetUserData;
		break;
	case WIDGET_DROPDOWN:
		pFuncAlign = DROPDOWN_SetTextAlign;
		pFuncFont = DROPDOWN_SetFont;
		pFuncPara = DROPDOWN_SetUserData;
		break;
	}

	if (pFuncPara != NULL)
		pFuncPara(hItem, &pPara, sizeof(pPARA_TCB));

	if (pFuncFont != NULL && pPara->guiTCB.pFont != NULL)
		pFuncFont(hItem, pPara->guiTCB.pFont);

	if (pFuncAlign != NULL && pPara->guiTCB.align >= 0x80)
		pFuncAlign(hItem, (pPara->guiTCB.align & 0x7F));
}


static void _cbClient(WM_MESSAGE * pMsg) {
	static int onOff = 1;
	int NCode;
	int Id;
	int size, index;
	int x0, y0;
	void *pBitmap;
	pPARA_TCB pPara, pBase;
	GUI_MEMDEV_Handle hMemdev;
	WM_HWIN hWin;
	GUI_RECT rect;
	WM_MESSAGE Msg;
	WM_PID_STATE_CHANGED_INFO info;


	WM_HWIN hItem;
	// USER START (Optionally insert additional variables)
	// USER END
	WM_GetUserData(pMsg->hWin, &pBase, sizeof(pPARA_TCB));
	pPara = pBase;

	switch (pMsg->MsgId) {
	case WM_INIT_DIALOG:
		//		if (pBase == pHomePage)
		//			WM_CreateTimer(pMsg->hWin, 1, 1000, 0);                           // 仅主页用

		if (pPara->parent == WM_HBKWIN)
			pPara->parent = pMsg->hWin;

		if (pPara->slideFlag == 1)
			WM_MOTION_SetMoveable(pMsg->hWin, WM_CF_MOTION_X, 1);
		else if (pPara->slideFlag == 2)
			WM_MOTION_SetMoveable(pMsg->hWin, WM_CF_MOTION_Y, 1);
		else if (pPara->slideFlag == 3)
			WM_MOTION_SetMoveable(pMsg->hWin, WM_CF_MOTION_X + WM_CF_MOTION_Y, 1);

		CallFunction(pBase, pPara, 0, 0);
		while (1)
		{
			pPara = pPara->pParallelNext;

			if (pPara != NULL)
			{
				pPara->parent = pMsg->hWin;
				pPara->guiState = GUI_STATE_VALID;

				hItem = WM_GetDialogItem(pMsg->hWin, pPara->guiTCB.id);

				if (pPara->guiTCB.winType == WIDGET_STATIC_TEXT && pPara->guiTCB.align == 0)
					pPara->guiTCB.align = GUI_TA_HCENTER | GUI_TA_VCENTER | 0x80;

				if (pPara->guiTCB.winType == WIDGET_SLIDE && pPara->guiTCB.specialInfo.slider.useFlag == 1)
				{
					SLIDER_SetRange(hItem, pPara->guiTCB.specialInfo.slider.rangeStart, pPara->guiTCB.specialInfo.slider.rangeEnd);
					SLIDER_SetWidth(hItem, pPara->guiTCB.specialInfo.slider.thumbWidth);
				}

				SetFontAndAlignAndParameter(hItem, pPara);
				//CreateSubWindow(hItem, pPara);

				if (pPara->guiTCB.pCurrentPicture > 0)
				{
					if (pPara->guiTCB.winType == WIDGET_BUTTON)
						BUTTON_SetSkin(hItem, ButtonRedrawSkin);
					else if (pPara->guiTCB.winType == WIDGET_SLIDE)
					{
						SLIDER_SetSkin(hItem, SliderRedrawSkin);
					}
					else if (pPara->guiTCB.winType == WIDGET_PROGBAR)
					{
						PROGBAR_SetSkin(hItem, PROGBARRedrawSkin);
						PROGBAR_SetValue(hItem, 30);
					}
				}

				if ((pPara->appTCB.funType & NORMAL_MASK) == TIME_FUNCTION || (pPara->appTCB.funType & NORMAL_MASK) == DATE_FUNCTION)
					AddTimeFunction(pPara);

				if (pPara->transparency == 1)
					WM_SetHasTrans(WM_GetDialogItem(pMsg->hWin, pPara->guiTCB.id));

				CallFunction(pBase, pPara, 0, 0);
			}
			else
				break;
		}

		break;
	case WM_TOUCH:
//		if (pMsg->Data.p != NULL)
//		{
//			Msg.MsgId = WM_TOUCH;
//			info.State = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->State;
//			info.x = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->x;
//			Msg.Data.p = pMsg->Data.p;
//			WM_SendMessage(pBase->parent, &Msg);
//		}
//		else
//		{
//			//pMsg->Data.p = 1;
//		}

		break;
	case WM_PID_STATE_CHANGED:
		/*Msg.MsgId = WM_PID_STATE_CHANGED;
		info.State = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->State;
		info.x = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->x;
		Msg.Data.p = pMsg->Data.p;
		WM_SendMessage(pBase->parent, &Msg);*/

		//if (pMsg->Data.p != NULL)
		//{
		//	Msg.MsgId = WM_PID_STATE_CHANGED;
		//	info.State = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->State;
		//	info.x = ((WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->x;
		//	Msg.Data.p = pMsg->Data.p;
		//	WM_SendMessage(pBase->parent, &Msg);
		//}
		//else
		//{
		//	pMsg->Data.p = 1;
		//}
		break;
	case WM_TIMER:
		CallFunction(pBase, pPara, 2, pMsg->Data.v);
		break;
	case WM_NOTIFY_PARENT:
		Id = WM_GetId(pMsg->hWinSrc);
		NCode = pMsg->Data.v;
		WM_GetUserData(pMsg->hWin, &pPara, sizeof(pPARA_TCB));

		switch (NCode) {
		case WM_NOTIFICATION_CLICKED:
			if (pPara->winLayer <= 1)
				pHomePage->guiTCB.isMoving = 0;
		case WM_NOTIFICATION_RELEASED:
            if (pPara->winLayer <= 1 && pHomePage->guiTCB.isMoving == 1)
				break;
            
			while (1)
			{
				pPara = pPara->pParallelNext;
				if (pPara != NULL)
				{
					if (pPara->guiTCB.id == Id)
					{
						if (GetEditMode() == EDIT_MODE_ENABLED)
						{
							hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
							//PopEditWindow(pPara);
						}
						else
						{
							pPara->appTCB.keyMsg = NCode;

							CallFunction(pBase, pPara, 1, 0);                                              // 调用用户应用程序

							switch ((pPara->appTCB.funType & SYSTEM_MASK))
							{
							case JUMP_TO_WINDOW:                                                           // 跳转到下一窗口
								if (NCode == WM_NOTIFICATION_RELEASED && pPara->pNextLayer != NULL)
								{
									pPara = pPara->pNextLayer;

									if (pBase != pEncryptionPage)
									{
										pPara->pPreviousLayer = pBase;

										if (*(uint16_t*)pPara->guiTCB.specialInfo.occupy != 0 &&
											pEncryptionPage->guiTCB.specialInfo.encryption.cipherOK == 0 &&
											pPara->guiTCB.specialInfo.encryption.cipherOK == 0 ||
											pEncryptionPage->guiTCB.specialInfo.encryption.cipherModify == 1)
										{
											pEncryptionPage->pNextLayer     = pPara;                       // 保存目标页
											pEncryptionPage->pPreviousLayer = pBase;
											pPara                           = pEncryptionPage;             // 插入密码页
										}
									}
									else if (pBase->guiTCB.specialInfo.encryption.cipherOK == 0 &&
											pPara->guiTCB.specialInfo.encryption.cipherOK == 0)           // 密码不正确
										break;

									if(pBase->winLayer > 1)                                                // 如果当前窗口不属于第一层，则隐藏
										WM_HideWindow(pBase->parent);

									if (pPara->winLayer <= 1)                                              // 如果跳转到一级界面，直接移动就行
										WM_MoveTo(pHomePage->parent, -pPara->guiTCB.xPos, 0);
									else
									{
										//CreateWidget(pPara, WM_HBKWIN, 1);
										WM_ShowWindow(pPara->parent);                                      // 如果跳转到其他层，则必须要调用函数显示窗口
									}
								}

								break;
							case RETURN_FUNCTION:                                                          
								if (NCode == WM_NOTIFICATION_RELEASED)
								{
									if (pPara->winLayer > 1)                                               // 如果大于1， 隐藏窗口
									{
										//DeleteSubPage(pMsg->hWin, pBase);
										WM_HideWindow(pPara->parent);
										pPara = pBase->pPreviousLayer;                                     // 指向新窗口数据结构

										if (pPara != NULL && pPara->winLayer > 1)                          // 如果新窗口不属于第一层，则必要调用函数来显示窗口
											WM_ShowWindow(pPara->parent);
									}
									else                                                                   // 如果在一级界面使用返回，则直接跳转到主页
										WM_MoveTo(pHomePage->parent, pHomePage->guiTCB.xPos, pHomePage->guiTCB.yPos);
								}
								break;
							case HOME_FUNCTION:                       // 跳转到主页
								if (NCode == WM_NOTIFICATION_RELEASED)
								{
									if (pPara->winLayer > 1)
										WM_HideWindow(pPara->parent);

									WM_MoveTo(pHomePage->parent, pHomePage->guiTCB.xPos, pHomePage->guiTCB.yPos);
								}
								break;
							default:
								break;
							}

							//CallFunction(pPara, 1);                // 调用用户应用程序


							if (pPara->guiTCB.winType == WIDGET_BUTTON && (pPara->appTCB.funType & SYSTEM_MASK) != RETURN_FUNCTION && (pPara->appTCB.funType & SYSTEM_MASK) != HOME_FUNCTION)
							{
								if (NCode == WM_NOTIFICATION_RELEASED)
								{
									pPara->appTCB.guiState = !pPara->appTCB.guiState;

									//if (pPara->appTCB.guiState == 1)
									//	pPara->guiTCB.pCurrentPicture = &pPara->guiTCB.pictureOn.bitMap;
									//else
									//	pPara->guiTCB.pCurrentPicture = &pPara->guiTCB.pictureOff.bitMap;
								}
							}
						}

						break;
					}
				}
				else
					break;
			}

			break;
			// USER START (Optionally insert additional code for further notification handling)
			// USER END
		case WM_NOTIFICATION_SEL_CHANGED:
			while (1)
			{
				pPara = pPara->pParallelNext;
				if (pPara != NULL)
				{
					if (pPara->guiTCB.id == Id){
						CallFunction(pBase, pPara, 3, 0);
						break;
					}
				}
				else
				{
					break;
				}
			}
			break;
		}
		break;
		// USER START (Optionally insert additional message handling)
		// USER END
	case WM_PAINT:
		if (pPara->winLayer > 1)
		{
			GUI_Clear();
			if (pPara->guiTCB.pictureBack.hMem > 0)
			{
				WM_GetWindowRect(&rect);
				GUI_MEMDEV_WriteOpaqueAt(pPara->guiTCB.pictureBack.hMem, rect.x0, rect.y0);
				//            GUI_DrawBitmap(&pPara->guiTCB.pictureBack.bitMap, 0, 0);
			}
			else
			{
				GUI_SetColor(GUI_BLACK);
				GUI_FillRect(pPara->guiTCB.xPos, pPara->guiTCB.yPos, pPara->guiTCB.xSize, pPara->guiTCB.ySize);
			}
		}

		if (pPara->guiTCB.pRedraw != NULL)
			pPara->guiTCB.pRedraw((void*)pPara);

		break;
	default:
		WM_DefaultProc(pMsg);
		break;
	}
}


/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       CreateWindow
*/
WM_HWIN CreateWidget(pPARA_TCB pParaTcb, WM_HWIN hParent, uint8_t showImmediate) {
	WM_HWIN hWin;
	WM_HWIN hClient;
	pPARA_TCB pPara = pParaTcb;
	WM_MESSAGE Msg = { 0 };
	GUI_WIDGET_CREATE_INFO *pInfo;
	uint16_t widgetNumber, layer;
	uint32_t size, slideFlag = 0;
	uint8_t *pBuffer;
	int8_t  ret = 0;
	GUI_HMEM hMem;
	GUI_MEMDEV_Handle hMemdev = 0;


	widgetNumber = GetWidgetNumber(pParaTcb);
	pInfo = (GUI_WIDGET_CREATE_INFO *)malloc(sizeof(GUI_WIDGET_CREATE_INFO) * widgetNumber);
	layer = pParaTcb->winLayer;

	for (uint16_t count = 0; count < widgetNumber; count++)
	{
		pInfo[count].pfCreateIndirect = (GUI_WIDGET_CREATE_FUNC*)GetWidgetCreateFunction(pPara->guiTCB.winType);
		if (pInfo[count].pfCreateIndirect == NULL)
		{
			widgetNumber = count;
			break;
		}

		pInfo[count].pName = pPara->guiTCB.winName;
		pInfo[count].Id = pPara->guiTCB.id;
		pInfo[count].x0 = pPara->guiTCB.xPos;
		pInfo[count].y0 = pPara->guiTCB.yPos;
		pInfo[count].xSize = pPara->guiTCB.xSize;
		pInfo[count].ySize = pPara->guiTCB.ySize;
		pInfo[count].Flags = 0;
		pInfo[count].Para = 0;
		pInfo[count].NumExtraBytes = sizeof(pPARA_TCB);

		//		pPara->guiTCB.pictureBack.hMem = hMemdev;

		//if (layer <= 1)
		{
			//ConfigureBitMap(pPara->guiTCB.backPath, &pPara->guiTCB.pictureBack, 1);

			//if(strlen(pPara->guiTCB.backPath) > 1)
			//ConfigureBackGradient(pPara);

			ConfigureBitMap(pPara->guiTCB.onPath, &pPara->guiTCB.pictureOn, 0);
			if (ConfigureBitMap(pPara->guiTCB.offPath, &pPara->guiTCB.pictureOff, 0))
				pPara->guiTCB.pCurrentPicture = &pPara->guiTCB.pictureOff.bitMap;
			else
				pPara->guiTCB.pCurrentPicture = NULL;
		}

		pPara = pPara->pParallelNext;
	}

	pParaTcb->parent = hParent;

	hWin = GUI_CreateDialogBox(pInfo, widgetNumber, 0, hParent, 0, 0);
	free(pInfo);

	WM_SetUserData(hWin, &pParaTcb, sizeof(pPARA_TCB));


	hClient = WM_GetClientWindow(hWin);              // Get handle of client window

	if (pParaTcb->transparency == 1)
		WM_SetHasTrans(hClient);                     // Set transparency

	WM_SetCallback(hClient, _cbClient);              // Overwrite callback
	Msg.MsgId = WM_INIT_DIALOG;                      // Send WM_INIT_DIALOG
	WM_SendMessage(hClient, &Msg);


	if (showImmediate == 0)
		WM_HideWindow(hWin);

	return hWin;
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
